﻿@using LightSO.CMS.Model.EasyUI

@{
    Datagrid Datagrid = null;
    if (ViewBag.Datagrid == null)
    {
        Datagrid = Datagrid.Create(null);
    }
    else
    {
        Datagrid = ViewBag.Datagrid;
    }
    string version = "20150605";
}

<script type="text/javascript">
    @RenderSection("EnumDefine", false)
    var columns_@(Datagrid.Id) = [
        @if(Datagrid.Columns.Count>0)
        {
        @:[
            var i = 0;
            foreach (var item in Datagrid.Columns)
            {
                Column col = item.Value;
                @:{
                    @:  field: '@(col.Id)', title: '@(col.Title)', width: @(col.Width), hidden: @((col.Id=="Id"?"true":(col.Hidden ? "true": "false"))), sortable: @(col.Sortable ? "true" : "false"), align: '@(col.Align.ToString().ToLower())',
                    @:  formatter: function(value, row, index){
                        string columnName = Datagrid.EnsureColumnName(Request.Path.Replace("/", "_"), col.Id, Datagrid.Id);
                        if (!string.IsNullOrWhiteSpace(columnName))
                        {
                            var script = RenderSection(string.Format("{0}ColumnFormat", columnName), false);
                            if(script != null)
                            {
                                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"</?script[^<>]*>");
                                @Html.Raw(regex.Replace(script.ToHtmlString(), string.Empty));
                                @:value = this.formatter(value, row, index);
                            }
                        }
                        @:return typeof(value) != "string" && value == 0 ? "" : value;
                    @:}
                @:}
                i++;
                if (i != Datagrid.Columns.Count)
                {
                    @:,
                }
            }
        @:]
        }
    ];

    var onClickRowEvent_@Datagrid.Id = function (index, row) {
            @{
                var onClickRowEvent = RenderSection("OnClickRow", false);
                if (onClickRowEvent != null)
                {
                    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"</?script[^<>]*>");
                    @Html.Raw(regex.Replace(onClickRowEvent.ToHtmlString(), string.Empty));
                    @:this.onClickRow(index, row);
                }
            }
    };


    var onDblClickRowEvent_@Datagrid.Id = function (index, row) {
            @{
                var onDblClickRowEvent = RenderSection("OnDblClickRow", false);
                if (onDblClickRowEvent != null)
                {
                    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"</?script[^<>]*>");
                    @Html.Raw(regex.Replace(onDblClickRowEvent.ToHtmlString(), string.Empty));
                    @:this.onDblClickRow(index, row);
                }
            }
    };

</script>

@if (!string.IsNullOrWhiteSpace(Datagrid.ScriptSrc))
{
    <script type="text/javascript" src="@Datagrid.ScriptSrc?version=@version"></script>
}

<table id="@(Datagrid.Id)" data-options="singleSelect:@(Datagrid.SingleSelect ? "true" : "false")"></table>
@RenderBody()

@*右键菜单*@
@if (!Datagrid.IsNoNeedToolBar)
{
    <div id="popupmenu_@(Datagrid.Id)" class="easyui-menu" style="width: 120px; display: none;">
        @RenderSection("PopupMenu", required: false)
        <div class="menu-sep"></div>
        <div id="menuItemReload" onclick="easymvc.datagridReload('@(Datagrid.Id)');" data-options="iconCls:'icon-reload'">刷新</div>
    </div>  
}


@*工具条面板组*@
@if (!Datagrid.IsNoNeedToolBar)
{
    <div id="datagrid_toolbar_@(Datagrid.Id)" style="display:none;">
        @*按钮组*@
        @{var buttons = RenderSection("ToolbarButtonsGroup", false);}
        @if (buttons != null)
        {
            <div class="datagrid-toolbar">
                @Html.Raw(buttons.ToHtmlString())
            </div>
        }
        @*筛选面版*@
        @{var filter = RenderSection("ToolbarFilter", false);}
        @if (filter != null)
        { 
            <div class="datagrid-toolbar">
                @Html.Raw(filter.ToHtmlString())
            </div>  
        }
        @*工具条菜单*@
        <div class="datagrid-toolbar" style="border-bottom-width:0px;">
            @RenderSection("ToolbarMenu", false)
            <a id="btnDatagridReload" onclick="easymvc.datagridReload('@(Datagrid.Id)');" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">刷新</a>
        </div>
    </div> 
}


<script type="text/javascript">
        $(function(){
            $("table[id='@(Datagrid.Id)']").parent(".datagrid-view").attr("onselectstart","return false").css({"-moz-user-select":"none"});
        });
</script>



